//-----------------------------------------------------------------------
// <copyright file="DeleteWebsite.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
// </copyright>
// <summary>Deletes the specified WebSite.</summary>
//-----------------------------------------------------------------------

namespace Microsoft.Sdc.Tasks.Web.WebSite
{
    using System;
    using Microsoft.Sdc.Tasks.Configuration.Web;

    /// <summary>
    /// Deletes an web site from the local server
    /// </summary>
    /// <remarks>
    /// <code><![CDATA[<Web.WebSite.DeleteWebsite (Description="description"  || 
    ///   Identifier="identifier">)]]></code>
    /// <para>where:</para>
    /// <para><i>description</i></para>
    /// <para>A description for the web site</para>
    /// <para><i>identifier</i></para>
    /// <para>A valid web site identifier</para>
    /// <para><b>You must specify either a description or an identifier</b></para>
    /// </remarks>
    /// <example>
    /// <code><![CDATA[
    /// <Project>
    ///     <Target Name="Test" >
    ///         <Web.WebSite.DeleteWebsite
    ///             Description="testwebsite" />
    ///         <Web.WebSite.DeleteWebsite
    ///             Identifier="654321" />
    ///     </Target>
    /// </Project>
    /// ]]></code>    
    /// </example>    
    public class DeleteWebsite : TaskBase
    {
        private string description;
        private string identifier;
        private string machineName = "localhost";

        /// <summary>
        /// Machine to operate on
        /// </summary>
        public string MachineName
        {
            get { return this.machineName; }
            set { this.machineName = value; }
        }

        /// <summary>
        /// Description of web site to delete
        /// </summary>
        /// <value>A valid web site description</value>
        /// <remarks>If this parameter is not specified you must specify the site <seealso cref="Identifier">Identifier</seealso> 
        /// for the site you wish to delete</remarks>
        public string Description
        {
            get { return (this.description ?? String.Empty); }
            set { this.description = value; }
        }

        /// <summary>
        /// web site idenfitier to delete
        /// </summary>
        /// <value>A valid web site identifier</value>
        /// <remarks>If this parameter is not specified you must specify the site <seealso cref="Description">Description</seealso> 
        /// for the site you wish to delete</remarks>
        public string Identifier
        {
            get { return (this.identifier ?? String.Empty); }
            set { this.identifier = value; }
        }

        /// <summary>
        /// Performs the action of this task.
        /// </summary>
        protected override void InternalExecute()
        {
            if (this.Description.Length == 0 && this.Identifier.Length == 0)
            {
                throw new TaskException("Web.DescriptionOrIdentifier");
            }

            if (this.Description.Length > 0 && this.Identifier.Length > 0)
            {
                throw new TaskException("Web.DescriptionOrIdentifierNotBoth", this.description, this.identifier);
            }

            int id = Utilities.CheckWebSiteDescriptionOrIdentifier(this.Description, this.Identifier, "Deleting", this.MachineName);

            if (string.IsNullOrEmpty(this.Description))
            {
                if (WebSite.Exists(this.MachineName, id))
                {
                    WebSite.Delete(id, this.MachineName);
                }
            }
            else
            {
                if (WebSite.Exists(this.MachineName, this.Description))
                {
                    WebSite.Delete(this.Description, this.MachineName);
                }
            }
        }
    }
}